/*
	       File: frmMain.cs
	Create Date: 2007-09-10
	    Version:

	Description:


	 Change log:


*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

using System.Workflow.Activities;
using System.Workflow.Runtime;

using GuerillaProgrammer.Components;
using GuerillaProgrammer.FileProcessor;

namespace FPTestHarness
{
    public partial class frmMain : Form
    {
        #region Private member fields
        private GuerillaProgrammer.FileProcessor.Settings.Configuration _Configuration;
        private string _SampleFilesPath;
        #endregion

        #region Constructors and cleanup methods
        public frmMain()
        {
            InitializeComponent();
            _SampleFilesPath = System.Configuration.ConfigurationManager.AppSettings["SampleFiles"];
            _Configuration = GuerillaProgrammer.FileProcessor.ConfigurationManager.ReadConfiguration(System.Configuration.ConfigurationManager.AppSettings["ConfigFilePath"]);
        }
        #endregion

        #region Private implementation methods
        private GuerillaProgrammer.FileProcessor.Settings.Configuration GetConfiguration()
        {
            GuerillaProgrammer.FileProcessor.Settings.Configuration config = new GuerillaProgrammer.FileProcessor.Settings.Configuration();
            GuerillaProgrammer.FileProcessor.Settings.ConfigurationDirectory dir = new GuerillaProgrammer.FileProcessor.Settings.ConfigurationDirectory();
            GuerillaProgrammer.FileProcessor.Settings.ConfigurationFileType file = new GuerillaProgrammer.FileProcessor.Settings.ConfigurationFileType();
            GuerillaProgrammer.FileProcessor.Settings.ConfigurationWorkflow wf = new GuerillaProgrammer.FileProcessor.Settings.ConfigurationWorkflow();

            //First add the workflow
            wf.ID = Guid.NewGuid().ToString();
            wf.Name = "test";
            wf.WorkflowStrongName = txtWorkflowName.Text;

            //Second create the file type
            file.ID = Guid.NewGuid().ToString();
            file.Description = "test";
            file.FilePattern = "*.*";
            file.Name = "Test";
            file.WorkflowID = wf.ID;

            //Third the directory
            dir.ID = Guid.NewGuid().ToString();
            dir.Description = "test";
            dir.DirectoryFileTypes = new List<string>();
            dir.DirectoryFileTypes.Add(file.ID);
            dir.Name = "Test";
            dir.Path = Path.GetDirectoryName(txtFilename.Text);

            //now setup the configuration
            config.Workflows = new List<GuerillaProgrammer.FileProcessor.Settings.ConfigurationWorkflow>();
            config.Workflows.Add(wf);
            config.FileTypes = new List<GuerillaProgrammer.FileProcessor.Settings.ConfigurationFileType>();
            config.FileTypes.Add(file);
            config.Directories = new List<GuerillaProgrammer.FileProcessor.Settings.ConfigurationDirectory>();
            config.Directories.Add(dir);
            config.Services = new GuerillaProgrammer.FileProcessor.Settings.ConfigurationServices();
            config.Services.ArchiveDirectory = _Configuration.Services.ArchiveDirectory;
            config.Services.AssemblyDirectory = _Configuration.Services.AssemblyDirectory;

            return config;
        }
        #endregion

        #region Class properties
        #endregion

        #region Events handlers
        private void btnExit_Click(object sender, EventArgs e)
        {
            Close();
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            lstMessages.Items.Clear();
            txtMessages.Text = "";
        }
        private void btnExecute_Click(object sender, EventArgs e)
        {
            try
            {
                GuerillaProgrammer.FileProcessor.Settings.Configuration config = GetConfiguration();
                GuerillaProgrammer.FileProcessor.Settings.ConfigurationFileType fileType = config.FileTypes[0];
                GuerillaProgrammer.FileProcessor.Settings.ConfigurationDirectory directory = config.Directories[0];

                WorkItem workItem = new WorkItem(txtFilename.Text, fileType.ID, directory.ID);

                FileProcessor fileProc = new FileProcessor(workItem, config);

                fileProc.ProcessWorkItem(null);
                lstMessages.Items.Add("Success!");
            }
            catch (Exception exc)
            {
                string message = string.Format("An error occured: {0}", exc.Message);
                lstMessages.Items.Add(message);
            }
        }
        private void btnOpenFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = _SampleFilesPath;
            ofd.Filter = "All files (*.*)|*.*|Text files (*.txt)|*.txt|Xml files (*.xml)|*.xml";

            if (DialogResult.OK == ofd.ShowDialog())
            {
                txtFilename.Text = ofd.FileName;
            }
        }
        private void btnOpenWorkflow_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory = _Configuration.Services.AssemblyDirectory;
            ofd.Filter = "assembly files (*.dll)|*.dll|All files (*.*)|*.*";
            
            if (DialogResult.OK == ofd.ShowDialog())
            {
                Assembly assembly = Assembly.LoadFile(ofd.FileName);
                List<Type> workflows = new List<Type>();
                Type[] assemblyTypes = assembly.GetTypes();

                //First find all of the workflows we are interested in
                foreach (Type asmType in assemblyTypes)
                {
                    if (true == asmType.IsSubclassOf(typeof(SequentialWorkflowActivity)))
                    {
                        workflows.Add(asmType);
                    }
                }

                //Now display the list and ask the user which one
                dlgSelectWorkflow selectWorkflow = new dlgSelectWorkflow(workflows);
                if (DialogResult.OK == selectWorkflow.ShowDialog())
                {
                    txtWorkflowName.Text = string.Format("{0},{1}", selectWorkflow.SelectedWorkflow, assembly.FullName);
                    lblAssemblyPath.Text = ofd.FileName;
                }
            }
        }
        #endregion
    }
}